package cn.cau.epidemicsurveysystem.service.impl;

import cn.cau.epidemicsurveysystem.bean.EpidMissionRecordBean;
import cn.cau.epidemicsurveysystem.bean.PatientTripBean;
import cn.cau.epidemicsurveysystem.dao.EpidMissionRecordRepository;
import cn.cau.epidemicsurveysystem.dao.PatientTripRepository;
import cn.cau.epidemicsurveysystem.service.TrackService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static jdk.nashorn.internal.runtime.regexp.joni.Syntax.Java;

/**
 * @projectName: gp2023
 * @package: cn.cau.epidemicsurveysystem.service.impl
 * @className: TrackServiceImpl
 * @author: zy
 * @description: TODO
 * @date: 2022/3/7 14:50
 * @version: 1.0
 */
@Service
public class TrackServiceImpl implements TrackService {
    @Autowired
    PatientTripRepository patientTripRepository;

    @Autowired
    EpidMissionRecordRepository missionRecordRepository;

    @Autowired
    RestTemplate restTemplate;

    /**
     * @param missionId: 任务id
      * @return List<List<Double>> ：返回该任务id对应轨迹上所有地点的经纬度
     * @author zy
     * @description TODO
     * @date 2022/3/7 17:06
     */
    @Override
    public List<List<Double>> getTrackByMissionId(int missionId) {
        List<PatientTripBean> tripBeans = patientTripRepository.findPatientTripBeansByMissionIdEqualsOrderByArrivalTime(missionId);
        String url = "https://api.map.baidu.com/place/v2/search?region=北京&output=json&ak=53oVIOgmSIejwV7EfphPgTynOZbIiVYu";
        List<List<Double>> locations = new ArrayList<>();
        for(PatientTripBean tripBean:tripBeans){
            String place = tripBean.getTripPlace();
            String cur_url = url+"&query="+place;
            System.out.println(cur_url);

            String result = restTemplate.getForObject(cur_url, String.class);
            JSONObject answersJson= JSON.parseObject(result);
            JSONArray results =(JSONArray) answersJson.get("results");
            JSONObject first =(JSONObject) results.get(0);
            JSONObject location = first.getJSONObject("location");
            double lng = location.getDouble("lng");
            double lat = location.getDouble("lat");
            List<Double> loca = new ArrayList<>();
            loca.add(lng);
            loca.add(lat);
            locations.add(loca);
        }
        return locations;
    }

    /**
     * @param diseaseId: 疾病id
    	 * @param type: 需要调查的类型，0-疑似病例，1-确诊病例
      * @return List<List<List<Double>>> ：该疾病的所有疑似/确诊病例移动轨迹经纬度
     * @author zy
     * @description TODO
     * @date 2022/3/7 17:07
     */
    @Override
    public List<List<List<Double>>> getTracksByDiseaseIdAndType(int diseaseId, int type) {
        List<EpidMissionRecordBean> missions;
        List<List<List<Double>>> tracks = new ArrayList<>();
        if(type==0) {
            missions = missionRecordRepository.findSuspectedCasesByDiseaseId(diseaseId);
        }
        else{
            missions = missionRecordRepository.findConfirmedCasesByDiseaseId(diseaseId);
        }

        for(EpidMissionRecordBean mission:missions){
            int mission_id = mission.getMissionId();
            tracks.add(getTrackByMissionId(mission_id));
        }
        return tracks;
    }

    @Override
    public List<List<Double>> getTrackByMissionIdAndDate(int missionId, String start, String end) {
        List<PatientTripBean> tripBeans = patientTripRepository.findPatientTripBeansByDate(missionId,start,end);
        String url = "https://api.map.baidu.com/place/v2/search?region=北京&output=json&ak=53oVIOgmSIejwV7EfphPgTynOZbIiVYu";
        List<List<Double>> locations = new ArrayList<>();
        for(PatientTripBean tripBean:tripBeans){
            String place = tripBean.getTripPlace();
            String cur_url = url+"&query="+place;
            System.out.println(cur_url);

            String result = restTemplate.getForObject(cur_url, String.class);
            JSONObject answersJson= JSON.parseObject(result);
            JSONArray results =(JSONArray) answersJson.get("results");
            JSONObject first =(JSONObject) results.get(0);
            JSONObject location = first.getJSONObject("location");
            double lng = location.getDouble("lng");
            double lat = location.getDouble("lat");
            List<Double> loca = new ArrayList<>();
            loca.add(lng);
            loca.add(lat);
            locations.add(loca);
        }
        return locations;
    }

    @Override
    public List<List<List<Double>>> getTracksByDiseaseIdAndTypeAndDate(int diseaseId, int type, String start, String end) {
        List<EpidMissionRecordBean> missions;
        List<List<List<Double>>> tracks = new ArrayList<>();
        if(type==0) {
            missions = missionRecordRepository.findSuspectedCasesByDiseaseId(diseaseId);
        }
        else{
            missions = missionRecordRepository.findConfirmedCasesByDiseaseId(diseaseId);
        }

        for(EpidMissionRecordBean mission:missions){
            int mission_id = mission.getMissionId();
            tracks.add(getTrackByMissionIdAndDate(mission_id,start,end));
        }
        return tracks;
    }


}
